// 如果你的 Node 版本 >=18，fetch 已经是内置
// 如果是低版本需要用 node-fetch 包

const API_KEY = "AIzaSyCJKqYQ8kqHGNxGfod8tH9y5XwA0waToOo";
const url = `https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key=${API_KEY}`;

const body = {
  contents: [
    {
      parts: [
        { text: "Explain how AI works in a few words" }
      ]
    }
  ]
};

(async () => {
  try {
    const res = await fetch(url, {
      method: "POST",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(body)
    });

    if (!res.ok) {
      console.error(`HTTP Error: ${res.status}`);
      const errorText = await res.text();
      console.error(errorText);
      return;
    }

    const data = await res.json();
    console.log(JSON.stringify(data, null, 2));
  } catch (err) {
    console.error("Error:", err);
  }
})();
